﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GosWeb.Models.Db;

namespace GosWeb.Controllers
{
    public class IllnessController : Controller
    {
        //
        // GET: /Gene/
        GosDBContext db = GosDBContext.GetInstance();

        public ActionResult Index()
        {
            var genes = from m in db.Illnesses select m;

            return View(genes.ToList());
        }

        //
        // GET: /Gene/Details/5

        public ActionResult Details(string id)
        {
            Illness biont = db.Illnesses.Find(id);
            if (biont == null)
            {
                return RedirectToAction("Index");
            }
            else
            {
                //Location originalValues = db.Entry(biont).ComplexProperty(u => u.Address).OriginalValue;    
                return View("Details", biont);
            }
        }

        //
        // GET: /Gene/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Gene/Create

        [HttpPost]
        public ActionResult Create(Illness newGene)
        {
            if (newGene == null || string.IsNullOrWhiteSpace( newGene.Code))
            {
                return View(newGene); 
            }

            newGene.Code = newGene.Code.ToUpper();
            var g = db.Illnesses.Find(newGene.Code);
            if (g != null)
            {
                ModelState.AddModelError("Code", "已被使用！请使用别的代号");
            }
            if (ModelState.IsValid)
            {
                db.Illnesses.Add(newGene);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                return View(newGene);
            }
        }

        //
        // GET: /Gene/Edit/5
        public ActionResult Edit(string id)
        {
            Illness g = db.Illnesses.Find(id);
            if (g == null)
                return RedirectToAction("Index");

            return View(g);
        }

        //
        // POST: /Gene/Edit/5
        [HttpPost]
        public ActionResult Edit(Illness model)
        {
            var g = db.Illnesses.Find(model.Code);
            if (g == null)
            {
                ModelState.AddModelError("Code", "未找到此 Illness！");
            }
            if (ModelState.IsValid)
            {
                UpdateModel(g);
                db.SaveChanges();
                return RedirectToAction("Details", new { id = model.Code });
            }
            else
            {
                return View(model);
            }
        }

        //
        // GET: /Gene/Delete/5

        public ActionResult Delete(string id)
        {
            Illness g = db.Illnesses.Find(id);
            if (g == null)
                return RedirectToAction("Index");
            return View(g);
        }

        //
        // POST: /Gene/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                Illness g = db.Illnesses.Find(id);
                db.Illnesses.Remove(g);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        
    }
}
